package io.dora.conf;

import io.dora.base.Protocol;
import io.dora.registry.RegistryIndex;

import java.util.Collection;
import java.util.Map;

import org.springframework.context.ApplicationContext;

/**
 * 服务配置基类
 *
 * @author zhaohongbo
 */
public abstract class AbstractServiceConfig {

	protected String id;
	
	protected Protocol protocol;
	
	protected RegistryIndex registry;
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	public Protocol getProtocol() {
		return protocol;
	}

	public void setProtocol(Protocol protocol) {
		this.protocol = protocol;
	}

	public RegistryIndex getRegistry() {
		return registry;
	}

	public void setRegistry(RegistryIndex registry) {
		this.registry = registry;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public <T> T getBeanInstance(ApplicationContext applicationContext, Class<T> clazz) {
		Map map = applicationContext.getBeansOfType(clazz);
		if (map != null && map.size() > 0) {
			Collection list = map.values();
			Object[] arr = list.toArray();
			return (T) arr[0];
		}
		return null;
	}
	
}
